var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    youhuinum:0.00,
    paynum:0.00,
    zhekou:10,
    xiaofei:0,
    shopinfo:{},
    latitude:'',
    longitude:'',
    shopid:'',
    yuanjia:0,
    placeholder:'请输入消费金额',
    value:'',
    leitab: 1,
    itemtab: 1,
    oil_info: '',
    shop_price: 0,    //店铺价
    system_price: 0, //系统价
    cut: 0,     //比国际价低
    xiaofei_num: 0,
    di: true,
    washinfo:'',
    bindtel: false,
    commentList:[],
    now_page:1,
    total_page:0,
    isCanPay:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      var scene = decodeURIComponent(options.scene);
      console.log(scene);
      var shopstr = scene.split('?');
      var shoparr = scene.split('=');
      var shopid = shoparr[1];
      this.setData({
        shopid:shopid
      })
      this.checkmobile();
    }else{
      var shopid = options.id;
      if(options.type){
        var type = options.type;
      }else{
        var type = 1;
      }
      
      this.setData({
        shopid: shopid,
        itemtab:type
      });
    }
    this.checkmobile();
    this.getaddress(shopid);
    this.getComment()
  },
  previewBanner(e){
    let index = e.currentTarget.dataset.index;
    let mentou = this.data.shopinfo.mentou;
    wx.previewImage({
      current: mentou[index], // 当前显示图片的http链接
      urls: mentou // 需要预览的图片http链接列表
    })
  },
// 获取评价列表
  getComment(now_page = 1){
    app.post({
      url: "api/shopr/carCommentList",
      data: {
        shop_id: this.data.shopid,
        now_page:now_page
      },
      success:(e)=> {
        console.log(e)
        if (e.data.code == 1) {
          let res = e.data.data;
          this.setData({
            commentList:this.data.commentList.concat(res.list),
            total_page:res.total_page
          })
        }
      }
    });
  },
  previewImage(e){
    console.log(e)
    let index = e.currentTarget.dataset.index;
    let url = e.currentTarget.dataset.url;
    let commentList = this.data.commentList;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: commentList[index].imgs // 需要预览的图片http链接列表
    })
  },
  // 检测是否绑定手机号
  checkmobile: function () {
    var status = wx.getStorageSync('status');
    status = parseInt(status);
    if (status == 0) {
      app.post({
        url:"api/userr/judgePhone",
        data:{
          user_id:wx.getStorageSync('userId')
        },
        success:res=>{
          if(res.data.data.need){
            this.setData({
              bindtel:true
            })
          }
        }
      })
    }
  },
  bindtel: function () {
    wx.navigateTo({
      url: '/pages/mine/mobile/mobile?type=20&shopid='+this.data.shopid,
    })
  },

  // 获取当前位置
  getaddress: function (shopid) {
    var that = this;
    var latitude = wx.getStorageSync('latitude');
    var longitude = wx.getStorageSync('longitude');
    if (latitude) {
      that.setData({
        latitude: latitude,
        longitude: longitude
      })
      that.getshop(shopid, longitude, latitude);
    } else {
      // 获取当前地理位置
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          console.log(res);
          var latitude = res.latitude
          var longitude = res.longitude
          that.setData({
            latitude: latitude,
            longitude: longitude
          })
          that.getshop(shopid, longitude, latitude);
        }
      })
    }
    
  },

  itemselect: function (e){
    var inx = e.currentTarget.dataset.index;
    this.setData({
      itemtab:inx
    })
    this.get_carprice(inx);
  },
  // 获取商家信息
  getshop:function(id,longitude,latitude){
    var that = this;
    var param = {
      url: "api/shopr/washShopInfo",
      data: {
        shop_id: id,
        longitude: longitude,
        latitude: latitude
      },
      success: function (e) {
        console.log(e)
        if (e.data.code == 1) {
          that.setData({
            shopinfo: e.data.data.shop_info,
            washinfo: e.data.data.car_info
          })
          var itemtab = that.data.itemtab;
          
      
          that.get_carprice(itemtab);
        }

      }
    }
    app.post(param);
  },

  get_carprice(tab){
    var washinfo = this.data.washinfo;
    for(var i=0;i< washinfo.length ; i++){
      if ( parseInt(washinfo[i].car_type) == parseInt(tab)){
        
        var di = false;
        console.log(washinfo[i].cut)
        if (washinfo[i].cut >= 0){
          di = true
        }
        washinfo[i].cut = Math.abs(washinfo[i].cut).toFixed(2);
        this.setData({
          shop_price: washinfo[i].shop_price,
          system_price: washinfo[i].system_price,
          cut: washinfo[i].cut,
          di:di,
          paynum: washinfo[i].system_price
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    this.checkmobile();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let now_page = this.data.now_page;
    console.log(now_page,this.data.total_page)
    if(now_page < this.data.total_page){
      now_page++;
      this.setData({
        now_page:now_page
      })
      this.getComment(now_page)
    }
},
  // 支付
  payit: function (e) {
    if(!this.data.isCanPay) return;
    var userid= wx.getStorageSync('userId');
    var shopid = this.data.shopid;
    var amount = this.data.paynum;
    var yuanjia = this.data.yuanjia;
    var shop_price = this.data.shop_price;
    var car_type = this.data.itemtab;
    var that = this;
    var param ={
      url:"api/shop/payWashMoney",
      data:{
        user_id: userid,
        shop_id: shopid,
        price: amount,
        shop_price: shop_price,
        car_type:car_type
      },
      success:function(e){
        // console.log(e);
        if(e.data.code == 1){
          var nonceStr = e.data.data.pay_data.nonceStr;
          var packagenum = e.data.data.pay_data.package;
          var paySign = e.data.data.pay_data.paySign;
          var signType = e.data.data.pay_data.signType;
          var timeStamp = e.data.data.pay_data.timeStamp;
          var order_id = e.data.data.order_id;
          wx.requestPayment({
            timeStamp: timeStamp,
            nonceStr: nonceStr,
            package: packagenum,
            signType: signType,
            paySign: paySign,
            success(res) {
              that.successpay(order_id)
            },
            fail(res) {

             }
          })
        }
      }
    }
    if(that.data.shopinfo.far){
      wx.showModal({
        cancelColor: '#999',
        cancelText:'继续支付',
        confirmColor:'red',
        confirmText:'重新选择',
        title:'温馨提醒',
        content:`您当前位置离该洗车店过远\n请确认是否继续支付`,
        success:(res)=>{
          console.log(res.cancel)
          if(res.confirm){
            console.log(res.cancel)
            wx.navigateBack({
              delta:1
            })
          }else{
            this.setData({
              isCanPay:false
            })
            app.post(param)
          }
        }
      })
    }else{
      this.setData({
        isCanPay:false
      })
      app.post(param)
    }
    
  },
  // 支付成功
  successpay:function(id){
      var that= this;
      var param = {
        url:"api/shopr/payWashSuccess",
        data:{
          order_id:id
        },
        success:function(e){
            wx.showToast({
              title: e.data.msg,
              icon:"none",
              duration:2000
            })
            that.setData({
              isCanPay:true
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '/pages/mine/otherCarOrderDetail/index?id=' + id,
              })
            },500)
            that.setData({
              xiaofei_num:0,
              paynum:0,
              value:''
            })
        }
      };
      app.post(param);
  },
  showdaohang: function () {
    var detail = this.data.shopinfo;
    console.log(detail)
    wx.openLocation({
      latitude: parseFloat(detail.latitude),
      longitude: parseFloat(detail.longitude),
      scale: 18,
      name: detail.shop_name,
      address: detail.address
    })
  },


})